package com.jia.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @RequestMapping("/userList")
    public List<Map<String, Object>> userList() {
        String sql = "select * from user";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }
    @RequestMapping("/addUser")
    public String  addUser() {
        String sql = "insert into user values(12,'s的','123')";
        jdbcTemplate.update(sql);
        return "add ok";
    }

    @RequestMapping("/updateUser/{id}")
    public String  updateUser(@PathVariable int id) {
        String sql = "update user set name=?,pwd=? where id = "+id;
        Object[] objects = new Object[2];
        objects[0] = "小水水水水";
        objects[1] = "666666";
        jdbcTemplate.update(sql,objects);
        return "update ok";
    }

    @RequestMapping("/deleteUser/{id}")
    public String  deleteUser(@PathVariable int id) {
        String sql = "delete from user where id = ?";
        jdbcTemplate.update(sql,id);
        return "delete ok";
    }

}
